package solutions;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-01-28
 * Time: 14:46
 */

//https://leetcode.cn/problems/squares-of-a-sorted-array/submissions/397500872/
class Solution7 {
    public int[] sortedSquares(int[] nums) {
        int[] ans = new int[nums.length];
        int pos = nums.length - 1;
        for (int i = nums.length - 1, j = 0; j <= i; ) {
            if (nums[j] * nums[j] > nums[i] * nums[i]) {
                ans[pos] = nums[j] * nums[j];
                pos--;
                j++;
            } else {
                ans[pos] = nums[i] * nums[i];
                pos--;
                i--;
            }
        }
        return ans;
    }
}
